package aom.ju.ss.view.dialog

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import aom.ju.ss.R
import aom.ju.ss.databinding.CxDialogProductModelLpBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.GoodsInfoData
import aom.ju.ss.mode.GoodsInfoDataSku
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.flowlayout.FlowLayout
import aom.ju.ss.view.base.flowlayout.TagAdapter
import aom.ju.ss.view.pay.CutPlaceAnOrder
import com.bumptech.glide.Glide
import com.google.gson.Gson

/**
 * 说明: 商品型号弹出框
 */
class CxAttrProductModelDialog(
    var mode: GoodsInfoData,
    activity: Activity,
    isaddCar: Boolean = false
) :
    BaseDialog(activity) {
    var binding: CxDialogProductModelLpBinding
    var num = 1
    var ckp = 0
    override fun isBottom(): Boolean {
        return true
    }

    init {
        setWidth(-1)
        binding = vBinding as CxDialogProductModelLpBinding
        binding.info = mode

        binding.etProductNum.addTextChangedListener(object : TextWatcherLP() {
            override fun afterTextChanged(s: Editable?) {
                super.afterTextChanged(s)
                s?.let {
                    if (s.toString().toIntOrNull() != null) {
                        num = s.toString().toInt()
                    }
                }
            }
        })

        binding.btnAddCart.VG(isaddCar)
        binding.btnBuy.VG(!isaddCar)


        Glide.with(activity)
            .load(mode.image)
            .placeholder(R.drawable.goods_def)
            .error(R.drawable.goods_def)
            .into(binding.ivProductImg)


        val tagAdapter: TagAdapter<GoodsInfoDataSku> =
            object : TagAdapter<GoodsInfoDataSku>(mode.skus) {
                override fun getView(
                    parent: FlowLayout?,
                    position: Int,
                    o: GoodsInfoDataSku?
                ): View? {
                    val rootview =
                        LayoutInflater.from(activity).inflate(R.layout.layout_product_sku, null)
                    val llRoot = rootview.findViewById<LinearLayout>(R.id.ll_root)
                    val iv = rootview.findViewById<ImageView>(R.id.iv_sku_img)
                    iv.VG(false)
                    val tv = rootview.findViewById<TextView>(R.id.iv_sku_value)
                    tv.text = o?.name
                    val ckd = ckp == position
                    llRoot.background =
                        ContextCompat.getDrawable(
                            activity,
                            if (ckd) R.drawable.bg_product_sku1 else R.drawable.bg_product_sku2
                        )
                    tv.setTextColor(
                        ContextCompat.getColor(
                            activity,
                            if (ckd) R.color.white else R.color.tc1
                        )
                    )
                    return rootview
                }
            }


        binding.llSkus.setOnSelectedListener { position: Int ->
            ckp = position
            tagAdapter.notifyDataChanged()
            binding.tvProductPrice.text = mode.skus!![position].price
            binding.tvProductInventory.text = "库存" + mode.skus!![position].inventory + "件"
        }
        binding.llSkus.adapter = tagAdapter
        binding.tvProductNumMinus.setOnClickListener {
            if (num >= 2) {
                num--
                binding.etProductNum.setText(num.toString())
            }
        }
        binding.tvProductNumAdd.setOnClickListener {
            if (num < 9999) {
                num++
                binding.etProductNum.setText(num.toString())
            } else {
                "最大9999".show()
            }
        }
        binding.btnAddCart.setOnClickListener {
//            ProductDetailsPresenter().addCart(
//                mode.activity_goods.id.toString(),
//                num.toString(),
//                list[ckp].id.toString(),
//                ""
//            )
        }


        binding.btnBuy.setOnClickListener {

            if (mode.skus == null || mode.skus!!.size == 0) {
                "此商品暂无规格无法购买".show()
                return@setOnClickListener
            }
            if (mode.skus!![ckp].inventory == 0 || num > mode.skus!![ckp].inventory!!) {
                "库存不足".show()
                return@setOnClickListener
            }

            val setModeList = arrayListOf<GoodsInfoData>()

            val sku = GoodsInfoDataSku()
            sku.name = mode.skus!![ckp].name
            sku.num = num.toString()
            sku.price = mode.skus!![ckp].price
            sku.id = mode.skus!![ckp].id

//            val setMode =
//                GoodsInfoData(id = mode.id, image = mode.image, goodsInfoShop_infoMode = mode.goodsInfoShop_infoMode,name = mode.name)

            val setMode = mode

            setMode.skus = arrayListOf(sku)
            setMode.goodsInfoShop_infoMode = mode.goodsInfoShop_infoMode

            setModeList.add(setMode)

            activity.startActivity(
                Intent(activity, CutPlaceAnOrder::class.java)
                    .putExtra("data", Gson().toJson(setModeList))
                    .putExtra("goodsType", mode.type.toString())
                    .putExtra("orderType", "1")
            )
            dis()
        }

        binding.btnAddCart.setOnClickListener {
            if (mode.skus == null || mode.skus!!.size == 0) {
                "此商品暂无规格无法加入购物车".show()
                return@setOnClickListener
            }
            if (mode.skus!![ckp].inventory == 0 || num > mode.skus!![ckp].inventory!!) {
                "库存不足".show()
                return@setOnClickListener
            }

            OKHttpUtils(activity)
                .SetApiUrl(LP_API.add_cart)
                .SetKey("goods_id", "num", "sku_id")
                .SetValue(mode.id.toString(), num.toString(), mode.skus!![ckp].id.toString())
                .POST(object : MyHttpCallBack {
                    override fun ok(jsonString: String?, httpTY: String?) {
                        "加入购物车成功".show
                        dis()
                    }

                    override fun error(e: String?, jsonString: String?, httpTY: String?) {
                        e?.show
                    }

                })
        }
    }

    override fun getLay(): Int {
        return R.layout.cx_dialog_product_model_lp
    }


}